<template>
  <div class="app-container">
    <el-row :gutter="10">
      <fold :span="5" :xs="24" :show.sync='sideshow'>
        <el-card shadow="never">
          <org-tree ref="orgTree" modelType="'dept','community','floor','unit','room'" @nodeClick="handleNodeClick"
                    @firstNodeClick="handleNodeClick"/>
        </el-card>
      </fold>
      <el-col :span="sideshow ? 19 : 24" :xs="24">
        <el-card shadow="never">
          <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
            <el-row :gutter="20">
              <el-col :span="5" :xs="24">
                <el-form-item label="业主名称" prop="customName">
                  <el-input
                    v-model="queryParams.customName"
                    placeholder="请输入业主名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="5" :xs="24">
                <el-form-item label="状态" prop="status">
                  <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                    <el-option
                      v-for="item in dict.join_register"
                      :key="item.dictValue"
                      :label="item.dictLabel"
                      :value="item.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="9" :xs="24">
                <el-form-item label="入伙时间" prop="joinTime">
                  <el-date-picker
                    style="width: 300px"
                    v-model="joinTime"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="入伙日期开始"
                    end-placeholder="入伙日期结束">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="5" :xs="24">
                <el-form-item label-width="0">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                         v-hasPermi="['waitress:joinRegister:add']">入伙登记
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                         v-hasPermi="['waitress:joinRegister:edit']">修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                         @click="handleDelete"
                         v-hasPermi="['waitress:joinRegister:remove']">删除
              </el-button>
            </el-col>
            <!--<el-col :span="1.5">-->
              <!--<el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"-->
                         <!--@click="handleExport" v-hasPermi="['waitress:joinRegister:export']">导出-->
              <!--</el-button>-->
            <!--</el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="joinRegisterList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="入伙单号" align="center" prop="houseContractNum" width="150">
              <template slot-scope="scope">
                <el-link @click="handleView(scope.row)" type="primary">{{scope.row.no}}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="业主名称" align="center" prop="customName" width="100"/>
            <el-table-column label="房产信息" align="left" prop="houseContractNum">
              <template slot-scope="scope">
                <span>{{ scope.row.communityName }}{{ scope.row.roomName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="入伙时间" align="center" prop="joinTime" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.joinTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="100">
              <template slot-scope="scope">
                {{ dict.label.join_register[scope.row.status] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.status =='10'"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-hasPermi="['waitress:joinRegister:auditing']"
                  @click="handleAuditing(scope.row)"
                >审核
                </el-button>
                <el-button
                  v-if="scope.row.status == '20'"
                  size="mini"
                  type="text"
                  icon="el-icon-circle-close"
                  v-hasPermi="['waitress:joinRegister:emigrationAud']"
                  @click="handleEmigration(scope.row)"
                >迁出
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>

    <JoinRegisterForm ref="formRef" @cancel="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listJoinRegister, delJoinRegister, exportJoinRegister,auditing,emigrationAud} from "@/api/waitress/joinRegister";
  import {getAdvanceMoney} from "@/api/payfee/advanceReceipt"
  import JoinRegisterForm from "./form.vue"

  export default {
    name: "JoinRegister",
    components: {JoinRegisterForm},
    mixins: [listMixin],
    dicts:[ 'join_register' ],
    data() {
      return {
        sideshow: true,
        // 入伙登记表格数据
        joinRegisterList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customName: null,
          floorId: null,
        },
        currentNode:{},
        //入伙时间
        joinTime:[],
        selection:[]
      };
    },
    created() {

    },
    methods: {
      /** 查询入伙登记列表 */
      getList() {
        this.loading = true;
        let queryParams = this.addDateRange(this.queryParams,this.joinTime,"joinTime")
        listJoinRegister(queryParams).then(response => {
          this.joinRegisterList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.joinTime = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.selection = selection
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.formRef.show({"type": "ADD","currentNode":this.currentNode});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let yshList = this.selection.filter(row=>row.status != '10')
        if(yshList.length > 0){
          let nos = yshList.map(item => item.no)
          this.msgWarning("入伙登记单["+nos+"]不是待确认状态,不能修改。")
          return false;
        }
        const id = row.id || this.ids
        const option = {
          "type": "UPDATE",
          "id": id,
          "currentNode":this.currentNode
        }
        this.$refs.formRef.show(option);
      },
      handleView(row) {
        const id = row.id || this.ids
        const option = {
          "type": "VIEW",
          "id": id,
          "currentNode":this.currentNode
        }
        this.$refs.formRef.show(option);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let yshList = this.selection.filter(row=>row.status != '10')
        if(yshList.length > 0){
          let nos = yshList.map(item => item.no)
          this.msgWarning("入伙登记单["+nos+"]不是待确认状态,不能删除。")
          return false;
        }
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除入伙登记编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delJoinRegister(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有入伙登记数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportJoinRegister(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      },
      handleNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      handleEmigration(row){
        getAdvanceMoney(row.roomId).then(response => {
          if (response.data>0){
            this.$confirm('该房间有预收金额"'+response.data+'"元，确认迁出?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function () {
              return emigrationAud(row);
            }).then(() => {
              this.getList();
              this.$refs.orgTree.refresh()
              this.msgSuccess("迁出成功");
            }).catch(() => {
            });
          }else {
            this.$confirm('是否确认迁出入伙单号为"' + row.no + '"的数据项?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function () {
              return emigrationAud(row);
            }).then(() => {
              this.getList();
              this.$refs.orgTree.refresh()
              this.msgSuccess("迁出成功");
            }).catch(() =>{})
          }
        })
        // this.$confirm('是否确认迁出入伙单号为"' + row.no + '"的数据项?', "警告", {
        //   confirmButtonText: "确定",
        //   cancelButtonText: "取消",
        //   type: "warning"
        // }).then(function () {
        //   return emigrationAud(row);
        // }).then(() => {
        //   this.getList();
        //   this.$refs.orgTree.refresh()
        //   this.msgSuccess("迁出成功");
        // }).catch(() => {
        // });
      },
      handleAuditing(row){
        const ids = row.id;
        this.$confirm('是否确认入伙登记编号为"' + row.no + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return auditing(ids);
        }).then(() => {
          this.getList();
          this.$refs.orgTree.refresh()
          this.msgSuccess("确认成功");
        }).catch(() => {
        });


      }
    }
  };
</script>
